import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueSetupExtend from 'vite-plugin-vue-setup-extend';

import Markdown from 'vite-plugin-md'
import prism from 'markdown-it-prism'
import anchor from 'markdown-it-anchor'
import { code, link } from 'md-powerpack'
const root = process.cwd()
export default defineConfig((data: any) => {
  let { command, mode } = data
  let env: any = null
  const isBuild = command === 'build'

  if (!isBuild) {
    env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  } else {
    env = loadEnv(mode, root)
  }
  return {
    plugins: [
      vue({
        include: [/\.vue$/, /\.md$/] // <--
      }),
      vueSetupExtend(),

      Markdown({
        markdownItOptions: {
          html: true,
          linkify: true,
          typographer: true
        },
        markdownItSetup(md) {
          // add anchor links to your H[x] tags
          md.use(anchor)
          // add code syntax highlighting with Prism
          md.use(prism)
        },
        builders: [link(), code()]
      }),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      createStyleImportPlugin({
        resolves: [ElementPlusResolve()],
        libs: [
          {
            libraryName: 'element-plus',
            esModule: true,
            resolveStyle: (name: string) => {
              return `element-plus/theme-chalk/${name}.css`
            }
          }
        ]
      })
    ],
    //解决“vite use `--host` to expose”
    base: './', //不加打包后白屏
    server: {
      proxy: {
        '/api': { //这里的api可以理解为后端接口里面的前缀，就是所有的地址前面都是http://aaa.com/api/xxx/xxx
          // target: 'http://jiangxi.kadadx.com/',//请求的目标地址
          // target: 'http://192.168.1.200:8081/',/请求的目标地址
          target: 'http://192.168.1.78:8081/',//请求的目标地址
          changeOrigin: true,//是否允许跨域
          rewrite: (path) => path.replace(/^\/api/, '')
          // pathRewrite: {
          //   '^/api': '' // 重写路径（表示请求的地址没有api前缀的时候）
          // }
        }
      },
      hmr: {
        overlay: false
      },
      host: 'localhost'
    },
    resolve: {
      //别名配置，引用src路径下的东西可以通过@如：import Layout from '@/layout/index.vue'
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, 'src'),
        },
      ],
    },
    css: {
      preprocessorOptions: {
        scss: {
          // additionalData: '@import "@/assets/css/minxins/index.scss";'
        }
      }
    },
    build: {
      outDir: env.VITE_OUT_DIR || 'dist',
    }
  }
});

